package org.six11.olive.collabclient;

import org.jdom.Element;
import org.six11.util.io.Message;
import org.six11.util.adt.MultiState;
import org.six11.util.pen.Pt;
import org.six11.util.gui.AWTMessageHandler;

/**
 * 
 *
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class PenMessageHandler extends AWTMessageHandler {

  /**
   * @param state
   */
  public PenMessageHandler(MultiState state) {
    super(state);
  }

  /**
   * 
   */
  @Override
  public void handleAWT(Message msg) {
    String who = msg.getParam("personID");
    Element data = msg.getRoot().getChild("pen_data");
    Pt pt = new Pt(Integer.parseInt(data.getAttributeValue("x")), Integer.parseInt(data
        .getAttributeValue("y")));
    pt.setTime(Long.parseLong(data.getAttributeValue("user_time")));
    String type = data.getAttributeValue("type");
    OliveState oliveState = (OliveState) state.getValue("olive state");
    if (type.equals("down")) {
      oliveState.addForeignSequenceBegin(pt, who);
    } else if (type.equals("up")) {
      oliveState.addForeignSequenceEnd(pt, who);
    } else if (type.equals("move")) {
      oliveState.addForeignSequenceProgress(pt, who);
    } else if (type.equals("hello")) {
      bug("Received notification: a user with session " + who + " is now here.");
    } else {
      bug("Unknown pen type: " + type);
    }
  }

}
